package com.presto.directo.utils

import com.presto.directo.net.getHttpMap
import com.presto.directo.net.sign
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

object ImageUtil {

    suspend fun upload(path: String): String {
        if (path.isEmpty()) return ""
        val imgMap = getHttpMap().sign(1)
        val builder = MultipartBody.Builder()
        builder.setType(MultipartBody.FORM)
        for (key in imgMap.keys) {
            builder.addFormDataPart(key, imgMap[key].toString())
        }
        val file = File(path)
        builder.addFormDataPart("file", file.name, file.asRequestBody("image/*".toMediaType()))
        return getApiService().uploadImage(builder.build()).getResult().file_id
    }


}